package com.shangma.cn.controller;

import com.shangma.cn.commons.http.AxiosResult;
import com.shangma.cn.commons.http.AxiosStatus;
import com.shangma.cn.commons.page.PageBean;
import com.shangma.cn.commons.valid.AddGroup;
import com.shangma.cn.commons.valid.UpdateGroup;
import com.shangma.cn.controller.base.BaseController;
import com.shangma.cn.dto.base.impl.BrandDTO;
import com.shangma.cn.entity.base.impl.Brand;
import com.shangma.cn.perm.HasPerm;
import com.shangma.cn.query.impl.BrandQuery;
import com.shangma.cn.service.BrandService;
import com.shangma.cn.transfer.base.impl.BrandTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;

/**
 * 开发人员: 辉哥真球帅
 * 联系方式: zhengzhoudaxuevip@163.com
 * 创建时间: 2021/10/7  16:42
 * 文件含义:
 */
@RestController
@RequestMapping("brand")
@RequiredArgsConstructor
public class BrandController extends BaseController {

    /*
    * 前后端分离的时候
    *  前端和后台 可能不是一个人写的
    *
    * 当前端访问后台的接口时
    *       1： 怎么知道请求路径的
    *       2： 怎么知道需要什么参数
    *       3： 怎么知道返回值的
    *
    * 此时我们前后端程序员需要一个接口文档
    *    1: 在线的
    *         swagger2 knife4j
    *
    *
    *    2: 纸质版的 （电子版的）
    *
    *
    *    3： postman
    *
    *
    *
    *
    *
    *
    *
    * */

    public final BrandService brandService;

    public final BrandTransfer brandTransfer;

    /**
     * 分页条件查询
     *
     * @return
     */
    @GetMapping
    @HasPerm(permSign = "base:brand:list")
    public AxiosResult<PageBean<BrandDTO>> searchPage(BrandQuery brandQuery) {
        return AxiosResult.success(brandService.searchPage(brandQuery));
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public AxiosResult<BrandDTO> getById(@PathVariable long id) {
        return AxiosResult.success(brandTransfer.toDTO(brandService.findOne(id)));
    }


    @PostMapping
    @HasPerm(permSign = "base:brand:add")
    public AxiosResult<Void> add(@RequestBody @Validated(AddGroup.class) Brand brand) {
        return toAjax(brandService.addEntity(brand));
    }
    @PutMapping
    @HasPerm(permSign = "base:brand:update")
    public AxiosResult<Void> update(@RequestBody @Validated(UpdateGroup.class) Brand brand) {
        return toAjax(brandService.updateEntity(brand));
    }

    @DeleteMapping("{id}")
    public AxiosResult<Void> deleteById(@PathVariable long id) {
        return toAjax(brandService.deleteById(id));
    }


    @DeleteMapping("batch/{ids}")
    @HasPerm(permSign = "base:brand:delete")
    public AxiosResult<Void> batchDelete(@PathVariable List<Long> ids) {
        return toAjax(brandService.deleteByIds(ids));
    }
}
